package com.atguigu.gmall.publisher.controller;


import com.atguigu.gmall.publisher.bean.CourseReviewReduce;
import com.atguigu.gmall.publisher.service.CourseReviewReduceService;
import com.atguigu.gmall.publisher.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CourseReviewContrller {

    @Autowired
    private CourseReviewReduceService courseReviewReduceService;

    @RequestMapping("/stats")
    public String getActivityStats(
            @RequestParam(value = "date", defaultValue = "1") Integer date) {
        if (date == 1) {
            date = DateUtil.now();
        }
        List<CourseReviewReduce> courseReviewReduce = courseReviewReduceService.getCourseReviewReduce(date);
        if (courseReviewReduce == null) {
            return "";
        }

        System.out.println("size"+ courseReviewReduce.size());
        String courseId = "null";
        Double courseAvgScore = 0.0;
        Double goodReview = 0.0;
        Long userCount = 0L;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"total\": 4,\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"课程id\",\n" +
                "        \"id\": \"courseId\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"课程平均评价分\",\n" +
                "        \"id\": \"courseAvgScore\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"评价客户数\",\n" +
                "        \"id\": \"userCount\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"好评率\",\n" +
                "        \"id\": \"goodReview\"\n" +
                "      }\n" +
                "    ] ," +
                " \"rows\": [");
        for (int i = 0; i < courseReviewReduce.size(); i++) {
             courseId = courseReviewReduce.get(i).getCourse_id();
            System.out.println(courseId);
            courseAvgScore = courseReviewReduce.get(i).getCourseAvgScore();
             goodReview = courseReviewReduce.get(i).getGoodReview();
             userCount = courseReviewReduce.get(i).getUserCount();
             stringBuilder.append("{\n" + "        \"courseId\": \"")
                     .append(courseId)
                     .append("\",\n")
                     .append("        \"courseAvgScore\": \"")
                     .append(courseAvgScore)
                     .append("\",\n")
                     .append("        \"userCount\": \"")
                     .append(userCount)
                     .append("\",\n")
                     .append("        \"goodReview\": \"")
                     .append(goodReview)
                     .append("\",\n")
                     .append("        \"url\": \"http://www.baidu.com\"\n")
                     .append("      }");
             if (i<courseReviewReduce.size()-1){
                 stringBuilder.append(",");
             }
        }
        stringBuilder.append("]\n" +
                "  }\n" +
                "}");
        String json = stringBuilder.toString();
        return json;
    }
}
